package com.sxf.util;

import java.sql.Connection;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 * Hibernate为4.0版本
 * 
 * @author ShiXiafeng
 * 
 */
public class HibernateSessionFactory {
	private static Configuration conf;
	private static SessionFactory sf;
	private static ThreadLocal<Session> tl = new ThreadLocal<Session>();
	static {
		conf = new Configuration();
		conf.configure(HibernateSessionFactory.class.getClassLoader()
				.getResource("hibernate.cfg.xml"));
		sf = conf.buildSessionFactory(new StandardServiceRegistryBuilder()
				.build());
	}

	public static Session getSession() {
		return sf.openSession();
	}

	public static Session getCurrentSession() {
		return sf.getCurrentSession();
	}

	public static Session getSessionTL() {
		Session s = tl.get();
		if (s == null) {
			s = sf.openSession();
			tl.set(s);
		}
		return s;
	}

	public static void closeSessionTL() {
		Session s = tl.get();
		if (s != null) {
			s.close();
			tl.set(null);
		}
	}

	public static Session openSession() {
		return sf.openSession();
	}

	public static Connection getConnection() {
		Connection con = null;
		
		return null;
	}

	public static void closeSessionFactory() {
		if (sf != null) {
			sf.close();
			sf = null;
		}

	}
}
